<?php
global $db;

try {
	$db = new SQLiteDatabase("dummychat.db", 0666, $sqerr);
}
catch (Exception $e) {
	echo "Database";
}

switch ($_POST['action'])
{
	case 'get_uid':
		return get_uid($_POST['uname']);
		
	case 'add_to_cl':
		$uid = get_uid($_POST['uname']);
		$cuid = get_uid($_POST['contact']);
		
		if (!$cuid) {            // Contact does not exist
			echo "Unknown";
			return;
		}
		
		if ($cuid == $uid) {     // Attempt to add him/herself
			echo "Self"
			return;
		}
		
		// Check for the contact already being in the list
		$fetch = $db -> query('SELECT UID FROM List'.$uid.' WHERE UID="'.$cuid.'"') -> fetch();
		if ($fetch['UID'] == $cuid) {
			echo "Present";
			return;
		}
		
		$res = $db -> query('INSERT INTO List'.$uid.' (UID) VALUES ("'.$cuid.'")');
		
		if ($res == FALSE) echo "Failure";
		else echo "Success";     // Making friends! :D
		
		return;
}
{
	$username = $_POST['uname'];
	
	try {
		$db = new SQLiteDatabase("dummychat.db", 0666, $sqerr);
		$db -> query('UPDATE UserDB SET status=0 WHERE username="'.$username.'"');
	}
	catch (Exception $e) {
		echo "Database";
	}
}

function get_uid($uname)
{
	global $db;
	
	$fetch = $db -> query('SELECT UID FROM UserDB WHERE username="'.$uname.'"') -> fetch();
	return $fetch['UID'];
}